<template>
  <div class="login">
    <div class="loginCard">
      <div class="title">运营中心</div>
      <div class="content">
        <el-form ref="ruleFormRef"  :model="ruleForm" :rules="rules"
          label-width="0px" class="demo-ruleForm">
          <el-form-item label="" prop="account">
            <div class="account d-flex items-center px-8 py-12">
              <img  class="icon" src="@/assets/images/loginUser.png" alt="">
              <el-input @keyup.enter="onLogin" v-model="ruleForm.account" placeholder="请输入账号" clearable></el-input>
            </div>
          </el-form-item>
          <el-form-item label="" prop="pwd">
            <div class="password d-flex items-center px-8 py-12">
              <img class="icon" src="@/assets/images/loginPass.png" alt="">
              <el-input @keyup.enter="onLogin" v-model="ruleForm.pwd" :type="showKey ? 'text' : 'password'" clearable placeholder="请输入密码"></el-input>
              <img style="width: 30px;cursor: pointer;" v-show="!showKey" src="@/assets/images/closeeyes.png" @click.stop="handleEye"/>
              <img style="width: 30px;cursor: pointer;" v-show="showKey" src="@/assets/images/openeyes.png" @click.stop="handleEye"/>
            </div>
          </el-form-item>
          <el-button :loading="loading" type="primary" @click="onLogin">登录</el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref,reactive ,onMounted,getCurrentInstance} from 'vue';
import { _saveStorage } from '@/utils/localStorage';
import { useRouter } from "vue-router";
import md5 from "md5";
const loading=ref(false);
const showKey=ref(false);
const router = useRouter();
const ruleFormRef = ref(null);
const { proxy } = getCurrentInstance();
const ruleForm = reactive({
  account: "",
  pwd: "",
});
const validateUserName = (rule, value, callback) => {
  if (!value) {
    callback();
  } else {
    const reg = /^[a-zA-Z0-9_-]{4,20}$/;
    if (reg.test(value)) {
      callback();
    } else {
      callback();
    }
  }
};
const validatePassword = (rule, value, callback) => {
  if (!value) {
    callback();
  } else {
    const reg = /^(?!\d+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
    if (reg.test(value)) {
      callback();
    } else {
      callback();
      // return callback(new Error('必须是6-16个英文字母和数字，需要包含两种'))
    }
  }
};
const rules = {
  account: [
    {
      required: true,
      message: "请输入您的账号",
      trigger: ["blur", "change"],
    },
    {
      validator: validateUserName,
      trigger: 'blur'
    }
  ],
  pwd: [
    {
      required: true,
      message: "请输入您的密码",
      trigger: "blur",
    },
    {
      validator: validatePassword,
      trigger: "blur",
    },
  ],
};
const onLogin=()=>{
  if (!ruleFormRef.value) return;
  ruleFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      proxy.$https.post("/login", {
        account: ruleForm.account,
        pwd: md5(ruleForm.pwd),
      }).then((res) => {
        let { code, message, data } = res;
        if (code == 200) {
          proxy.$message.success("登录成功");
          _saveStorage("info", data);
          router.push("/index");
        } else {
          proxy.$message.error(message);
        }
        loading.value = false;
      }).catch((err) => {
        loading.value = false;
      });
    } else {
      return false;
    }
  });
}
const handleEye=()=>{
  showKey.value=!showKey.value;
}
</script>

<style lang="scss" scoped>
.login{
  width:100%;
  height:100vh;
  background:#f2f2f2;
  position:relative;
  font-family: 'Arial Negreta', 'Arial Normal', 'Arial';
  .loginCard{
    position:absolute;
    top:50%;
    left:50%;
    transform:translate(-50%,-50%);
    .title{
      font-weight: 700;
      font-style: normal;
      font-size: 42px;
      color: var(--el-color-primary);;
      margin-bottom: 45px;
      text-align: center;
    }
    .content{
      width: 520px;
      background:#fff;
      padding: 56px;
      border-radius: 3px;
      box-sizing: border-box;
      .account{
        border-bottom: 1px solid #f2f2f2;
        width: 100%;
      }
      .password{
        border-bottom: 1px solid #f2f2f2;
        width: 100%;
      }
      :deep .el-input__inner{
        height: 36px;
        line-height: 36px;
        border:none;
        padding-right: 30px;
      }
      :deep(.el-input__wrapper) {
        box-shadow: none;
      }
      :deep .el-button{
        width: 100%;
        margin-top: 15px;
        min-height:40px;
        font-size:14px;
      }
    }
  }
}
</style>